{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": "00000-fc24b67c-a12f-41d8-8c3e-c9e888bb9777",
    "output_cleared": false
   },
   "source": [
    "# Plot dose depth dose and uncertainty\n",
    "\n",
    "Use the output of the following simulation:\n",
    "- Folder: dosimetry/\n",
    "- Macros: ex1.mac\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "cell_id": "00001-882fa32f-4a7f-4f36-bb4f-22a941cc781b",
    "execution_millis": 185,
    "execution_start": 1605009448424,
    "output_cleared": false,
    "source_hash": "1a4b3bc8"
   },
   "outputs": [],
   "source": [
    "# Tell Jupyter to plot figure right in the page\n",
    "%matplotlib widget\n",
    "\n",
    "# Module with plot capabilities\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Module with scientific computing functions (matrix/vector)\n",
    "import numpy as np                \n",
    "\n",
    "# Modules with reading/write folder/file functions\n",
    "import os\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "cell_id": "00002-be9465ba-cedc-43ae-af91-c6ba51dd33d4",
    "execution_millis": 3,
    "execution_start": 1605009448612,
    "output_cleared": false,
    "source_hash": "909ee571"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The Current Working Directory (CWD) is: \n",
      " /Users/dsarrut/src/gate/dqprm/2021/gate-exercices/dosimetry\n"
     ]
    }
   ],
   "source": [
    "# The following command display the current working directory (where jupyter has been launched)\n",
    "cwd = os.getcwd()\n",
    "print('The Current Working Directory (CWD) is: \\n', cwd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "cell_id": "00003-0c8f5f1c-8c7c-4733-b2b7-8adb092edbec",
    "execution_millis": 9,
    "execution_start": 1605009455240,
    "output_cleared": false,
    "scrolled": true,
    "source_hash": "6a7e5f90"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The folder and contains the following files:\n",
      "output_ref/3d-pat-proton-Dose-Uncertainty.raw\n",
      "output_ref/3d-pat-Edep.mhd\n",
      "output_ref/profile-pat-Edep.txt\n",
      "output_ref/3d-pat-proton-Edep.raw\n",
      "output_ref/depth-pat-Edep.txt\n",
      "output_ref/profile-pat-Edep-Squared.txt\n",
      "output_ref/depth-pat-Edep-Uncertainty.txt\n",
      "output_ref/gamma-stat.txt\n",
      "output_ref/gamma-profile-Edep-Squared.txt\n",
      "output_ref/profile-pat-proton-Edep-Squared.txt\n",
      "output_ref/profile-pat-proton-Edep.txt\n",
      "output_ref/profile-pat-proton-Edep-Uncertainty.txt\n",
      "output_ref/gamma-depth-Edep-Uncertainty.txt\n",
      "output_ref/3d-pat-proton-Edep.mhd\n",
      "output_ref/depth-pat-proton-Dose.txt\n",
      "output_ref/depth-pat-proton-Edep-Squared.txt\n",
      "output_ref/3d-pat-Edep.raw\n",
      "output_ref/3d-pat-proton-Dose-Uncertainty.mhd\n",
      "output_ref/3d-pat-Edep-Squared.raw\n",
      "output_ref/3d-pat-proton-Dose-Squared.mhd\n",
      "output_ref/gamma-depth-Edep.txt\n",
      "output_ref/3d-pat-proton-Dose.mhd\n",
      "output_ref/depth-pat-proton-Edep.txt\n",
      "output_ref/3d-pat-Edep-Uncertainty.mhd\n",
      "output_ref/gamma-profile-Edep.txt\n",
      "output_ref/3d-pat-proton-Edep-Squared.raw\n",
      "output_ref/depth-pat-Edep-Squared.txt\n",
      "output_ref/stat-pat-proton.txt\n",
      "output_ref/profile-pat-proton-Dose.txt\n",
      "output_ref/3d-pat-proton-Edep-Uncertainty.mhd\n",
      "output_ref/readme.txt\n",
      "output_ref/3d-pat-proton-Edep-Uncertainty.raw\n",
      "output_ref/gamma-depth-Edep-Squared.txt\n",
      "output_ref/gamma-profile-Edep-Uncertainty.txt\n",
      "output_ref/depth-pat-Dose.txt\n",
      "output_ref/stat-pat.txt\n",
      "output_ref/3d-pat-proton-Edep-Squared.mhd\n",
      "output_ref/3d-pat-Edep-Uncertainty.raw\n",
      "output_ref/3d-pat-proton-Dose.raw\n",
      "output_ref/profile-pat-Dose.txt\n",
      "output_ref/3d-pat-proton-Dose-Squared.raw\n",
      "output_ref/profile-pat-Edep-Uncertainty.txt\n",
      "output_ref/3d-pat-Edep-Squared.mhd\n",
      "output_ref/depth-pat-proton-Edep-Uncertainty.txt\n"
     ]
    }
   ],
   "source": [
    "# Display the content of a folder\n",
    "output_folder = Path('./output_ref')                            # <--------------------------------------TO CHANGE BY YOUR OWN OUTPUT FOLDER \n",
    "if (not output_folder.is_dir()):\n",
    "    print('ERROR: {} is not a folder.'.format(output_folder))\n",
    "else:\n",
    "    print('The folder and contains the following files:'.format(output_folder))\n",
    "    p = output_folder.glob('**/*')\n",
    "    files = [x for x in p if x.is_file()]\n",
    "    for x in files:\n",
    "        print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "cell_id": "00004-9031347c-ed4a-4423-9d92-89f5365be3ca",
    "execution_millis": 3,
    "execution_start": 1605009455687,
    "output_cleared": false,
    "source_hash": "6ef66445"
   },
   "outputs": [],
   "source": [
    "# Set the filenames (d = depth, p = profile)\n",
    "f_d_edep = os.path.join(output_folder, 'gamma-depth-Edep.txt')\n",
    "f_d_uncert = os.path.join(output_folder, 'gamma-depth-Edep-Uncertainty.txt')\n",
    "f_p_edep = os.path.join(output_folder, 'gamma-profile-Edep.txt')\n",
    "f_p_uncert = os.path.join(output_folder, 'gamma-profile-Edep-Uncertainty.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "cell_id": "00005-b78e25ae-3f16-4ae9-9d9e-7451ae67725a",
    "execution_millis": 10,
    "execution_start": 1605009457518,
    "output_cleared": false,
    "source_hash": "1944e72c"
   },
   "outputs": [],
   "source": [
    "# Load data from txt files\n",
    "d_edep = np.loadtxt(f_d_edep)\n",
    "d_uncert = np.loadtxt(f_d_uncert)\n",
    "p_edep = np.loadtxt(f_p_edep)\n",
    "p_uncert = np.loadtxt(f_p_uncert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "cell_id": "00006-a088ea2b-9a69-4a6d-bc70-a183083a81a7",
    "execution_millis": 975,
    "execution_start": 1605009459176,
    "output_cleared": false,
    "source_hash": "a14e9386"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0f150c373c1c448da554651121310db7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Declare a single figure (one row, one column)\n",
    "fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(12, 6))\n",
    "\n",
    "# X values from 0 to n, n is the length of d_edep\n",
    "n = len(d_edep)\n",
    "x = np.linspace(0, n, n)\n",
    "\n",
    "# First curve, gamma depth in green\n",
    "y = d_edep\n",
    "c1 = ax.plot(x, y, 'g-', label='edep', linewidth=2)\n",
    "\n",
    "# Second curve, gamma uncertainty in blue, share the same x axis, but use a\n",
    "# different y axis\n",
    "y = d_uncert\n",
    "ax2 = ax.twinx()\n",
    "c2 = ax2.plot(x, y, 'b-', label='$\\sigma$ (uncertainty)')\n",
    "\n",
    "# Add the legend and the title\n",
    "lns = c1+c2\n",
    "labs = [l.get_label() for l in lns]\n",
    "ax.legend(lns, labs, loc=0)\n",
    "ax.set_title('Depth deposited energy')\n",
    "ax.set_xlabel('Distance in mm')\n",
    "ax.set_ylabel('Deposited energy in MeV')\n",
    "f = ax2.set_ylabel('Uncertainty')\n",
    "\n",
    "# save on disk (optional)\n",
    "plt.savefig('depth_dose.png',dpi=300,bbox_inches=\"tight\",pad_inches=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "cell_id": "00007-8bfaf02b-cc27-4374-a9e9-b271a7760145",
    "execution_millis": 246,
    "execution_start": 1605009460266,
    "output_cleared": false,
    "source_hash": "7f4c6660"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "28208320f0d0477f9da93b76b6ef50f8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Declare a figure \n",
    "fig, ax = plt.subplots(ncols=1, nrows=1, figsize=(12, 6))\n",
    "\n",
    "# Third curve, gamma profile in green\n",
    "n = len(p_edep)\n",
    "x = np.linspace(0, n, n)\n",
    "y = p_edep\n",
    "c3 = ax.plot(x, y, 'g-', label='edep', linewidth=2)\n",
    "\n",
    "# Last curve, gamma uncertainty profile\n",
    "y = p_uncert\n",
    "ax2 = ax.twinx()\n",
    "c4 = ax2.plot(x, y, 'b-', label='$\\sigma$ (uncertainty)')\n",
    "\n",
    "# Add the legend and the title\n",
    "lns = c3+c4\n",
    "labs = [l.get_label() for l in lns]\n",
    "ax.legend(lns, labs, loc=0)\n",
    "ax.set_title('Deposited energy profile')\n",
    "ax.set_xlabel('Distance in mm')\n",
    "ax.set_ylabel('Deposited energy in MeV')\n",
    "f = ax2.set_ylabel('Uncertainty')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cell_id": "00008-d351388e-e779-4bec-82b8-ed24999dc354",
    "output_cleared": false
   },
   "source": []
  }
 ],
 "metadata": {
  "deepnote_execution_queue": [],
  "deepnote_notebook_id": "97ba8bac-eb79-4668-ab41-70a851c67caa",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
